package cn.itsource.sm.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.sm.domain.Systemdictionarytype;
import cn.itsource.sm.query.SystemdictionarytypeQuery;
import cn.itsource.sm.service.ISystemdictionarydetailService;
import cn.itsource.sm.service.ISystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdictionarytype")
public class SystemdictionarytypeController {

    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;

    @GetMapping("/{id}")
    public Systemdictionarytype findOne(@PathVariable("id") Long id) {
        return systemdictionarytypeService.selectById(id);
    }

    @GetMapping
    public List<Systemdictionarytype> findAll() {
        return systemdictionarytypeService.selectAll();
    }

    /**
     * 添加和修改：Put localhost:8080/systemdictionarytype
     * @param systemdictionarytype
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarytype systemdictionarytype) {
        try {
            if (systemdictionarytype.getId() == null) {
                systemdictionarytypeService.save(systemdictionarytype);
            } else {
                systemdictionarytypeService.update(systemdictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 删除：Delete localhost:8080/systemdictionarytype
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            System.out.println(id);
            systemdictionarytypeService.delete(id);
            systemdictionarydetailService.delByTypes_id(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 分页查询：post localhost:8080/systemdictionarytype
     * @param systemdictionarytypeQuery
     * @return
     */
    @PostMapping
    public PageBean<Systemdictionarytype> pageQuery(@RequestBody SystemdictionarytypeQuery systemdictionarytypeQuery) {
        return systemdictionarytypeService.queryPage(systemdictionarytypeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delList")
    public AjaxResult patchDelete(@RequestBody List<Systemdictionarytype> ids) {
        try {
            for(Systemdictionarytype id:ids){
                systemdictionarytypeService.delete(id.getId());
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

}